package com.tyx.chargingpile.service.impl;

import com.tyx.chargingpile.Enum.ServiceMessageEume;
import com.tyx.chargingpile.service.ChargingPileService;
import com.tyx.chargingpile.service.MessageService;
import com.tyx.chargingpile.util.Util;
import com.tyx.chargingpile.vo.SocketResponseResult;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 电价下发下行
 */
@Service("chargingPileElectricPriceDown")
public class ChargingPileElectricPriceDown implements ChargingPileService {

    private final static Logger LOGGER = LoggerFactory.getLogger(ChargingPileElectricPriceDown.class);

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, byte[] context) throws Exception {
        return null;
    }

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, Map data) throws Exception {
        Map<String,String> dwn = new HashMap<>();
        // 从后台传过来的价格，单位是元
        String defaultPrice = (String) data.get("DefaultPrice");
        String firstTime = (String) data.get("FirstTime");
        String firstPrice = (String) data.get("FirstPrice");
        String secondTime = (String) data.get("SecondTime");
        String secondPrice = (String) data.get("SecondPrice");
        String thirdTime = (String) data.get("ThirdTime");
        String thirdPrice = (String) data.get("ThirdPrice");
        String fourthTime = (String) data.get("FourthTime");
        String fourthPrice = (String) data.get("FourthPrice");
        String pileNum = (String) data.get("PileId");
        // 将价格转换为分单位,并且前补0，补足6位
        defaultPrice = Util.formatDoubleTOInt(Double.parseDouble(defaultPrice),100) + "";
        defaultPrice = Util.FronFillZero(defaultPrice,6);
        firstPrice = Util.formatDoubleTOInt(Double.parseDouble(firstPrice),100) + "";
        firstPrice = Util.FronFillZero(firstPrice,6);
        secondPrice = Util.formatDoubleTOInt(Double.parseDouble(secondPrice),100) + "";
        secondPrice = Util.FronFillZero(secondPrice,6);
        if (!TextUtils.isEmpty(thirdPrice)){
            thirdPrice = Util.formatDoubleTOInt(Double.parseDouble(thirdPrice),100) + "";
            thirdPrice =  Util.FronFillZero(thirdPrice,6);
        }
        if (!TextUtils.isEmpty(fourthPrice)){
            fourthPrice = Util.formatDoubleTOInt(Double.parseDouble(fourthPrice),100) + "";
            fourthPrice = Util.FronFillZero(fourthPrice,6);
        }
        // 下发数据
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(defaultPrice).append(firstPrice).append(firstTime).append(secondPrice).append(secondTime);
        if (!TextUtils.isEmpty(thirdTime)){
            stringBuffer.append(thirdPrice).append(thirdTime);
        }
        if (!TextUtils.isEmpty(thirdTime)){
            stringBuffer.append(fourthPrice).append(fourthTime);
        }
        String elextricPrice = stringBuffer.toString().trim();
        dwn.put("elextricPrice",elextricPrice);
        byte[] dwnContext = MessageService.getMessage(dwn, ServiceMessageEume.ELECTRICPRICE.getDwnLinePath());
        ByteBuf pingMessage = Unpooled.buffer();
        pingMessage.writeBytes(dwnContext);
        channelHandlerContext.writeAndFlush(pingMessage);
        String commandId = ServiceMessageEume.ELECTRICPRICE.getCode();
        LOGGER.info("客户端"+channelHandlerContext.channel().id().toString()+",指令控制编号："+commandId + ",桩编号:" + pileNum +",前置电价下发" + Util.BCD2String(dwnContext));
        return new SocketResponseResult(channelHandlerContext,dwnContext);
    }
}
